Table of Contents
1. Introduction to Arrays
An array is a fundamental data structure in C that allows storing a collection of elements of the same data type in contiguous memory locations. A single-dimensional array (also called a one-dimensional array) is the simplest form of array.
Key Characteristics:
- Fixed size: The size must be known at compile time
- Homogeneous: All elements must be of the same data type
- Contiguous memory: Elements are stored in consecutive memory locations
- Zero-based indexing: First element is at index 0
- Random access: Elements can be accessed directly using their index
Why Use Arrays?
- Efficient storage of multiple values of the same type
- Easy access to elements using index
- Memory efficiency compared to individual variables
- Simplifies code for handling collections of data
2. Array Declaration
Syntax
data_type array_name[array_size];
Examples
int numbers[10]; // Array of 10 integers
float temperatures[5]; // Array of 5 floating-point numbers
char name[20]; // Array of 20 characters
Important Notes:
- The size must be a constant integer expression
- Array size cannot be changed after declaration
- Memory is allocated at compile time for static arrays
3. Array Initialization
Arrays can be initialized at the time of declaration.
Complete Initialization
int numbers[5] = {1, 2, 3, 4, 5};
float scores[4] = {95.5, 88.0, 92.3, 78.9};
char vowels[5] = {'a', 'e', 'i', 'o', 'u'};
Partial Initialization
int values[5] = {10, 20, 30}; // First 3 elements initialized, rest set to 0
int arr[5] = {1, 2}; // Equivalent to: {1, 2, 0, 0, 0}
Implicit Size Declaration
int numbers[] = {1, 2, 3, 4, 5}; // Size is 5
char message[] = "Hello"; // Size is 6 (includes null terminator)
4. Accessing Array Elements
Array elements are accessed using the array name followed by the index in square brackets.
Syntax
array_name[index]
Examples
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Access individual elements
printf("First element: %d\n", numbers[0]); // Output: 10
printf("Third element: %d\n", numbers[2]); // Output: 30
printf("Last element: %d\n", numbers[4]); // Output: 50
// Modify array elements
numbers[1] = 25;
printf("Modified second element: %d\n", numbers[1]); // Output: 25
// Using variables as indices
int index = 3;
printf("Element at index %d: %d\n", index, numbers[index]); // Output: 40
return 0;
}
Important Notes:
- Array indices start at 0, not 1
- The last element is at index
size - 1 - Accessing elements outside the array bounds leads to undefined behavior
5. Array Operations
Input/Output Operations
#include <stdio.h>
int main() {
int arr[5];
int i;
// Reading values into array
printf("Enter 5 integers:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
// Printing array elements
printf("Array elements: ");
for(i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Basic Operations
#include <stdio.h>
int main() {
int numbers[5] = {5, 2, 8, 1, 9};
int sum = 0, max, min;
int i;
// Finding sum
for(i = 0; i < 5; i++) {
sum += numbers[i];
}
printf("Sum: %d\n", sum);
// Finding maximum
max = numbers[0];
for(i = 1; i < 5; i++) {
if(numbers[i] > max) {
max = numbers[i];
}
}
printf("Maximum: %d\n", max);
// Finding minimum
min = numbers[0];
for(i = 1; i < 5; i++) {
if(numbers[i] < min) {
min = numbers[i];
}
}
printf("Minimum: %d\n", min);
return 0;
}
6. Arrays and Functions
Arrays can be passed to functions. When passed, arrays decay to pointers, so the function receives a pointer to the first element.
Passing Arrays to Functions
#include <stdio.h>
// Function to print array elements
void printArray(int arr[], int size) {
int i;
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Function to modify array elements
void multiplyByTwo(int arr[], int size) {
int i;
for(i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printf("Original array: ");
printArray(numbers, 5);
multiplyByTwo(numbers, 5);
printf("After multiplication: ");
printArray(numbers, 5);
return 0;
}
7. Common Array Operations
Searching (Linear Search)
#include <stdio.h>
// Linear search function
int linearSearch(int arr[], int size, int key) {
int i;
for(i = 0; i < size; i++) {
if(arr[i] == key) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int numbers[10] = {5, 12, 7, 3, 9, 15, 1, 8, 10, 6};
int key, index;
printf("Enter a number to search: ");
scanf("%d", &key);
index = linearSearch(numbers, 10, key);
if(index != -1) {
printf("Element found at index %d\n", index);
} else {
printf("Element not found\n");
}
return 0;
}
8. Memory Representation
Arrays are stored in contiguous memory locations. The array name acts as a pointer to the first element.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
printf("Array elements and their memory addresses:\n");
for(i = 0; i < 5; i++) {
printf("arr[%d] = %d, Address: %p\n", i, arr[i], &arr[i]);
}
printf("\nArray name as pointer: %p\n", arr);
printf("Address of first element: %p\n", &arr[0]);
return 0;
}
9. Common Errors and Pitfalls
Index Out of Bounds
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[5]); // Error: Valid indices are 0 to 4
Assuming Array Size in Functions
void printArray(int arr[]) {
// Wrong: sizeof(arr) gives pointer size, not array size
int size = sizeof(arr) / sizeof(arr[0]);
// ...
}
Forgetting to Initialize Arrays
int arr[5];
// Accessing uninitialized elements leads to undefined behavior
printf("%d", arr[0]);
10. Best Practices
Use Named Constants for Array Sizes
#define MAX_SIZE 100
// or
const int MAX_SIZE = 100;
int arr[MAX_SIZE];
Always Check Array Bounds
for(int i = 0; i < MAX_SIZE; i++) {
// Safe access
}
Initialize Arrays Properly
// Initialize all elements to 0
int arr[100] = {0};
Use sizeof() for Array Size Calculations
int arr[10];
int size = sizeof(arr) / sizeof(arr[0]); // Correct way to get size